package com.example.essayjoke.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.example.essayjoke.R;

/**
 * <p>Title: LetterSideBarView</p>
 * <p>Description: </p>
 * <p>Copyright: DS Technology Co.Ltd  Copyright (c) 2020</p>
 * <p>Company: www.ds.cn</p>
 *
 * @author wuguangcheng@ds.cn
 * @version 1.0
 * @date 2021/8/9 16:07
 */
public class LetterSideBarView extends View {
    private Paint paint;
    private Paint highLightPaint;
    private int letterSize = 12;
    private int letterColor = Color.BLACK;
    private String currentTouchLetter;
    private TouchLetterListener listener;

    private String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};

    public LetterSideBarView(Context context) {
        this(context, null);
    }

    public LetterSideBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LetterSideBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSideBarView);
        letterSize = (int) array.getDimensionPixelSize(R.styleable.LetterSideBarView_letterSize, 12);
        letterColor = array.getColor(R.styleable.LetterSideBarView_letterColor,  Color.BLACK);
        array.recycle();

        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(letterColor);
        // 设置的是像素
        paint.setTextSize(letterSize);

        // 高亮画笔
        highLightPaint = new Paint();
        highLightPaint.setAntiAlias(true);
        highLightPaint.setColor(Color.BLUE);
        highLightPaint.setTextSize(letterSize);
    }

    public void setListener(TouchLetterListener listener) {
        this.listener = listener;
    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 高度
        int height = MeasureSpec.getSize(heightMeasureSpec);

        // 宽度  （左右的padding和字母的宽度 ）
        int width = (int) (getPaddingLeft() + getPaddingRight() + paint.measureText("A"));

        setMeasuredDimension(width, height);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / letters.length;

        for (int i = 0; i < letters.length; i++) {
            String letter = letters[i];
            // 宽度/2-文字宽度/2
            int x = (int) (getWidth() / 2 - paint.measureText(letter) / 2);
            int letterCenterY = i * itemHeight + itemHeight / 2 + getPaddingTop();

            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            int dy = (int) ((fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent);
            int baseLine = letterCenterY + dy;

            // 当前字母高亮
            if (letter.equals(currentTouchLetter)) {
                canvas.drawText(letter, x, baseLine, highLightPaint);
            } else {
                canvas.drawText(letter, x, baseLine, paint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float fingerY = event.getY();
                float singleHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / letters.length;
                int position = (int) ((fingerY-getPaddingTop()) / singleHeight);
                // 相同字母不做处理
                if (position < letters.length && letters[position].equals(currentTouchLetter)) {
                    return true;
                }
                if (position < letters.length) {
                    currentTouchLetter = letters[position];
                    if (listener != null) {
                        listener.touch(currentTouchLetter);
                    }

                    Log.d("测试", currentTouchLetter);
                }
                break;
            case MotionEvent.ACTION_UP:
                // 消失非常快，可以延时消失
                currentTouchLetter = null;
                if (listener != null) {
                    listener.touch(null);
                }
                break;
        }
        invalidate();
        return true;
    }


    public interface TouchLetterListener {
        void touch(CharSequence letter);
    }
}
